import QtQuick 2.2

Rectangle {

    property string textname: ""        //文本内容
    property int textHLocate:0          //水平位置参数
    property int textVLocate:0          //垂直位置参数
    property string backcolor: "white"       //背景色
    property string clickcolor: "white"      //点击变色
    property string presscolor: "white"
 //边框颜色和宽度
    property string bordercolor:"white"
    property int borderwidth: 0

    property int fontsize: 22           //字体大小
    property string fontcolor: "black"
    property bool isSelect: false

    signal onclick(bool click, string title)
    signal titleChanged(string title)

    width: 100; height: 50
    color: backcolor
    border.color: textname === ""?"white":bordercolor; border.width: borderwidth;

    Text {
        id: name;
        text: textname;
        font.pixelSize: fontsize;
        color: fontcolor;
        elide: Text.ElideRight
        wrapMode: Text.WordWrap
        horizontalAlignment:{
            switch(textHLocate)
            {
            case 0:
                horizontalAlignment:Text.AlignLeft
                break;
            case 1:
                horizontalAlignment:Text.AlignHCenter
                break;
            case 2:
                horizontalAlignment:Text.AlignRight
                break
            }
        }

        verticalAlignment: {
            switch(textVLocate)
            {
            case 0:
                verticalAlignment:Text.AlignTop
                break;
            case 1:
                verticalAlignment:Text.AlignVCenter
                break
            case 2:
                verticalAlignment:Text.AlignBottom
                break
            }
        }


        onTextChanged:
        {
            if(text === "")
            {
                parent.visible = false
            }
            else
            {
                parent.visible = true
            }
            titleChanged(text)
        }

        anchors
        {
            top: parent.top; bottom: parent.bottom; left: parent.left; right: parent.right;
            leftMargin: 10
        }

       // anchors.centerIn: parent

    }

    MouseArea{
        onClicked: {
            console.log(qsTr("clicked the item "+ textname))
            if(isSelect)
            {
                color = backcolor
                isSelect = false
            }
            else
            {
                color = clickcolor
                isSelect = true
            }

            console.log(textname + "高和宽: "+ height+ " , " + width)

            onclick(isSelect, textname)
        }

        onPressed: {
            color = presscolor
        }

        onReleased: {
            color = backcolor
        }

        anchors.fill: parent
    }
}
